package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;


    public List<SpecGroupDTO> querySpecGroupByCategoryId(Long cid) {
        // 拼接条件
        SpecGroup record = new SpecGroup();
        record.setCid(cid);
        List<SpecGroup> specGroupList = specGroupMapper.select(record);
        // 判空
        if(CollectionUtils.isEmpty(specGroupList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 把SpecGroup 转成DTO
        return BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);
    }

    public List<SpecParamDTO> querySpecParam(Long gid, Long cid, Boolean searching) {

        //判断：三个不能同时为空
        if (gid == null && cid == null && searching == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }

        SpecParam record = new SpecParam();
        record.setCid(cid); // 分类id
        record.setGroupId(gid); // 规格组id
        record.setSearching(searching); // 是否搜索
        List<SpecParam> specParamList = specParamMapper.select(record);
        // 判空
        if(CollectionUtils.isEmpty(specParamList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 转成DTO
        return BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
    }

    /**
     * 查询规格组和组内的参数
     * @param id
     * @return
     */
    public List<SpecGroupDTO> querySpecByCategoryId(Long id) {
        // 1、获取规格组
        List<SpecGroupDTO> groupDTOList = querySpecGroupByCategoryId(id); //sql
        /*for (SpecGroupDTO specGroupDTO : groupDTOList) {
            // 问题：for循环中去查询规格参数，性能不好
            List<SpecParamDTO> specParamDTOS = querySpecParam(specGroupDTO.getId(), null, null);
            specGroupDTO.setParams(specParamDTOS);
        }*/
        // 2、根据分类id查询所有的规格参数
        List<SpecParamDTO> specParamDTOS = querySpecParam(null, id, null); // sql
        // 3、对规格参数进行分组： 以group_id来分组
        Map<Long, List<SpecParamDTO>> listMap = specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        // 4、把规格参数放入组中
        for (SpecGroupDTO specGroupDTO : groupDTOList) {
            specGroupDTO.setParams(listMap.get(specGroupDTO.getId()));
        }



        /*// 把组id分为一组
        Map<Long, List<SpecParamDTO>> listMap = new HashMap<>();
        for (SpecParamDTO spd : specParamDTOS){
            Long groupId = spd.getGroupId();
            List<SpecParamDTO> specList = listMap.get(groupId);
            if(CollectionUtils.isEmpty(specList)){
                specList = new ArrayList<>();
                specList.add(spd);
                listMap.put(groupId, specList);
            }else{
                specList.add(spd);
                listMap.put(groupId, specList);
            }
        }*/

        return groupDTOList;
    }
}
